# Immigration and the top 1%

# Top shares calculations


## preamble

# load packages
library(tidyverse)
library(future)
library(furrr)
library(fst)
library(readxl)
library(haven)
library(kableExtra)

# parallel
plan(multiprocess)

# options
options(future.globals.maxSize = 1000*2048^2)

# load functions
source('fn_topshare_calc.R')
source('fn_top_share_tables.R')

# upload population control totals: ONS statistics
#  from https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/bulletins/annualmidyearpopulationestimates/mid2020
pop_control_tot <- read_csv('pop_control_totals.csv',
                            col_types = cols_only(pop_tot_15plus = col_double(),
                                                  pop_tot_18plus = col_double(),
                                                  year = col_double()))

# size of percentile
psize <- pop_control_tot %>%
  filter(year == y) %>%
  select(pop_tot_18plus) %>%
  as_vector()/100

# round
psize <- round(psize, digits = 0)

# # # #                    

## parameters to change as needed

# years
yy <- 1997:2018

# top shares
ts_vec <- c(1, 0.1, 0.01, 0.001)*(1/100)

# age limit
age_limit <- 18

# migrant dummy, in the format quo(migrant_comb)
migrant_dummy_name <- quo(migrant_comb) 

# rank variable inside quos(.), for example quos(ti, ti_posttax)
rank_quos <- quos(ti)


#####################################  

## RUN, BUT DO NOT CHANGE

# safe check
rm(data)

# sample
which_sample <- 'SA_only'

# version
version <- paste(age_limit, 'plus', sep = '')

# Variable for population control total
var_control <- paste("pop_tot_", version, sep = "")


#####################################  

# Run the analysis
source('1_produce_topshares.R')
source('2_proportion_cross_section.R')
source('3_characteristics_distribution.R')
source('4_persistence_at_top.R')
source('5_year_of_arrival.R')
source('6_stayers.R')

#bring in ONS data
source('7_combine_ons_migrant_data.R')

# Make the figs
source('graph_code_1')
source('graph_code_2')
